package com.redtree.air.db
{
	public class Notes extends Model
	{
		public function Notes()
		{
			super();
			dataGridColumnsAry = [['title','标题'],['content','内容'],['dateString','记录日期'],['securityName','安全级别']];
			tableName = getTableName('notes');
		}
		public function getNotes(offset:int=0,limit:int=DEFAULT_PAGE_SIZE,underSecurity:Boolean=true,justCurrentLevel:Boolean=false):Array
		{
			var sql:String = "select n.*,s.name as security_name from notes as n " +
				"left join security as s on s.level_id=n.security_level";
			if( underSecurity )
				sql += " where n.security_level "+(justCurrentLevel?"=":"<=")+" "+currentSecurityLevel;
			sql += " ORDER BY n.id DESC";
			if( limit )
				sql += " limit "+limit+" offset "+offset;			
			return _getNotesBySql(sql);
		}
		public function _getNotesBySql(sql:String):Array
		{
			if(Model.instance.query(sql))var result:Array = Model.instance.data;
			var resultAry:Array = new Array();
			for( var i:int=0;i<result.length; i++ )
				resultAry.push(new Note(result[i]));
			return resultAry;
		}
		public function getLevelReferData(offset:int=0,limit:int=DEFAULT_PAGE_SIZE):Array
		{
			return getNotes(offset,limit,true,true);
		}
		public function getReferTotalCount():int
		{
			return getTotalCount(true,true);
		}
		public function getTotalCount(underSecurity:Boolean=true,justCurrentLevel:Boolean=false):int{
			var sql:String = "select count(*) as num from notes as n";
			if( underSecurity )
				sql += " where n.security_level "+(justCurrentLevel?"=":"<=")+" "+currentSecurityLevel;
			Model.instance.query(sql);
			
			return Model.instance.data[0]['num'];
			
		}
		/**
		 * 添加记事本
		 */
		public function addNote( title:String, content:String, securityLevel:int )
		{
			var sql:String = "INSERT INTO notes (title,content,security_level) values "
				+'("'+title+'","'+content+'",'+securityLevel+')';
			return Model.instance.query(sql);
		}
	}
}